// 定义 key 常量，防止写错
const TOKEN_KEY = 'token';
const USER_KEY = 'userId';

/**
 * 保存 token
 */
export const setToken = (token) => {
  wx.setStorageSync(TOKEN_KEY, token);
};

/**
 * 获取 token
 */
export const getToken = () => {
  return wx.getStorageSync(TOKEN_KEY);
};

/**
 * 删除 token
 */
export const removeToken = () => {
  wx.removeStorageSync(TOKEN_KEY);
};

/**
 * 保存 userId
 */
export const setUserId = (userId) => {
  wx.setStorageSync(USER_KEY, userId);
};

/**
 * 获取 userId
 */
export const getUserId = () => {
  return wx.getStorageSync(USER_KEY);
};

/**
 * 删除 userId
 */
export const removeUserId = () => {
  wx.removeStorageSync(USER_KEY);
};

/**
 * 判断是否已登录
 * 逻辑：有 token 且有 userId 视为已登录
 */
export const isLoggedIn = () => {
  const token = getToken();
  const userId = getUserId();
  return !!(token && userId);
};